<template>
  <div class="login-container">
    <!-- 导航部分 -->
    <van-nav-bar title="登录" >
      <template #left>
        <img class="logo" src="@/assets/images/logo.png" alt="">
      </template>
    </van-nav-bar>
<!-- 表单部分 -->
    <van-form @submit="loginFn">
      <van-field
        v-model="loginForm.mobile"
        name="手机号"
        label="手机号"
        placeholder="请输入手机号"
        :rules="[{ required: true, message: '请填写手机号',trigger:'onChange', pattern: /^1[3-9]\d{9}$/, }]"
      />
      <van-field
        v-model="loginForm.code"
        type="password"
        name="验证码"
        label="验证码"
        placeholder="请输入验证码"
        :rules="[{ required: true, trigger:'onChange', pattern: /^\d{6}$/, message: '请输入6位验证码' }]"
      />
      <div style="margin: 16px;">
        <van-button
        round
         block
        :loading='isLoadng'
        type="info"
        loading-text="加载中..."
         native-type="submit"
         >提交</van-button>
      </div>
    </van-form>
  </div>
</template>

<script>
import { getLoginAPI } from '@/api/Login'
import { setToken, setRefreshToken } from '@/utils/token'
export default {
  name: 'Login',
  data () {
    return {
      loginForm: {
        mobile: '18339693070',
        code: '246810'
      },
      isLoadng: false
    }
  },
  // created: {

  // },
  methods: {
    async loginFn () {
      try {
        this.isLoadng = true
        const res = await getLoginAPI(this.loginForm)
        const { data: { token, refresh_token } } = res
        setToken(token)
        // console.log(token)
        setRefreshToken(refresh_token)
        // console.log(refresh_token)
        // 主要通知
        this.$notify({ type: 'success', message: '登录成功' })
        this.$router.replace('/')
      } catch (e) {
        this.$notify({ type: 'warning', message: '登录失败 手机号或验证码不对' })
      } finally {
        this.isLoadng = false
      }
    }
  }
}
</script>

<style scoped lang="less">
.logo {
  width:100px;
  height:30px;
}
// .van-nav-bar {
//    background: #007bff;
// }

// /* /deep/ 就是把data-v-hash值选择器写到类名的前面 */
// /deep/ .van-nav-bar__title{
//   color: white;
// }
</style>
